package leetcode100;

import java.util.Deque;
import java.util.LinkedList;

public class lc012 {
    public static void main(String[] args) {
        Tree tree = new Tree();
        TreeNode root = tree.root;
        tree.visit();
        boolean flag = isSymmetric(root);
        System.out.println(flag);

    }

    public static boolean isSymmetric(TreeNode root) {
        if (root == null) return true;

        LinkedList<TreeNode> deque = new LinkedList<>();
        deque.addFirst(root.left);
        deque.addLast(root.right);
        TreeNode firstNode = null;
        TreeNode lastNode = null;


        while (!deque.isEmpty()) {
            System.out.println(deque);
            firstNode = deque.pollFirst();
            lastNode = deque.pollLast();

            if (firstNode == null && lastNode != null) return false;
            if (firstNode != null && lastNode == null) return false;
            if (firstNode == null && lastNode == null) continue;
            if (firstNode.val == lastNode.val) {
                deque.addFirst(firstNode.left);
                deque.addFirst(firstNode.right);
                deque.addLast(lastNode.right);
                deque.addLast(lastNode.left);
                continue;
            }
            return false;
        }
        return true;
    }
}
